<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- 
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License. 
-->
<html>
<head>
    <link type="text/css" rel="stylesheet" href="https://struts.apache.org/css/default.css">
    <style type="text/css">
        .dp-highlighter {
            width:95% !important;
        }
    </style>
    <style type="text/css">
        .footer {
            background-image:      url('https://cwiki.apache.org/confluence/images/border/border_bottom.gif');
            background-repeat:     repeat-x;
            background-position:   left top;
            padding-top:           4px;
            color:                 #666;
        }
    </style>
    <link href='https://struts.apache.org/highlighter/style/shCoreStruts.css' rel='stylesheet' type='text/css' />
    <link href='https://struts.apache.org/highlighter/style/shThemeStruts.css' rel='stylesheet' type='text/css' />
    <script src='https://struts.apache.org/highlighter/js/shCore.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushPlain.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushXml.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushJava.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushJScript.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushGroovy.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushBash.js' type='text/javascript'></script>
    <script type="text/javascript">
        SyntaxHighlighter.defaults['toolbar'] = false;
        SyntaxHighlighter.all();
    </script>
    <script type="text/javascript" language="javascript">
        var hide = null;
        var show = null;
        var children = null;

        function init() {
            /* Search form initialization */
            var form = document.forms['search'];
            if (form != null) {
                form.elements['domains'].value = location.hostname;
                form.elements['sitesearch'].value = location.hostname;
            }

            /* Children initialization */
            hide = document.getElementById('hide');
            show = document.getElementById('show');
            children = document.all != null ?
                    document.all['children'] :
                    document.getElementById('children');
            if (children != null) {
                children.style.display = 'none';
                show.style.display = 'inline';
                hide.style.display = 'none';
            }
        }

        function showChildren() {
            children.style.display = 'block';
            show.style.display = 'none';
            hide.style.display = 'inline';
        }

        function hideChildren() {
            children.style.display = 'none';
            show.style.display = 'inline';
            hide.style.display = 'none';
        }
    </script>
    <title>Struts 1 Solutions</title>
</head>
<body onload="init()">
<table border="0" cellpadding="2" cellspacing="0" width="100%">
    <tr class="topBar">
        <td align="left" valign="middle" class="topBarDiv" align="left" nowrap>
            &nbsp;<a href="home.html">Home</a>&nbsp;&gt;&nbsp;<a href="guides.html">Guides</a>&nbsp;&gt;&nbsp;<a href="migration-guide.html">Migration Guide</a>&nbsp;&gt;&nbsp;<a href="struts-1-solutions.html">Struts 1 Solutions</a>
        </td>
        <td align="right" valign="middle" nowrap>
            <form name="search" action="https://www.google.com/search" method="get">
                <input type="hidden" name="ie" value="UTF-8" />
                <input type="hidden" name="oe" value="UTF-8" />
                <input type="hidden" name="domains" value="" />
                <input type="hidden" name="sitesearch" value="" />
                <input type="text" name="q" maxlength="255" value="" />
                <input type="submit" name="btnG" value="Google Search" />
            </form>
        </td>
    </tr>
</table>

<div id="PageContent">
    <div class="pageheader" style="padding: 6px 0px 0px 0px;">
        <!-- We'll enable this once we figure out how to access (and save) the logo resource -->
        <!--img src="/wiki/images/confluence_logo.gif" style="float: left; margin: 4px 4px 4px 10px;" border="0"-->
        <div style="margin: 0px 10px 0px 10px" class="smalltext">Apache Struts 2 Documentation</div>
        <div style="margin: 0px 10px 8px 10px"  class="pagetitle">Struts 1 Solutions</div>

        <div class="greynavbar" align="right" style="padding: 2px 10px; margin: 0px;">
            <a href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=14240">
                <img src="https://cwiki.apache.org/confluence/images/icons/notep_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Edit Page"></a>
            <a href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=14240">Edit Page</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/listpages.action?key=WW">
                <img src="https://cwiki.apache.org/confluence/images/icons/browse_space.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Browse Space"></a>
            <a href="https://cwiki.apache.org/confluence/pages/listpages.action?key=WW">Browse Space</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=WW&fromPageId=14240">
                <img src="https://cwiki.apache.org/confluence/images/icons/add_page_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Add Page"></a>
            <a href="https://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=WW&fromPageId=14240">Add Page</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=WW&fromPageId=14240">
                <img src="https://cwiki.apache.org/confluence/images/icons/add_blogentry_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Add News"></a>
            <a href="https://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=WW&fromPageId=14240">Add News</a>
        </div>
    </div>

    <div class="pagecontent">
        <div class="wiki-content">
            <div id="ConfluenceContent"><p>This page is meant to be a list of issues and solutions that users ave encountered while migrating existing Struts 1 application to Struts 2, or developing new Webwork-based applications. If you have a solution to the issue that will of course be most helpful, but even if you don't it is worth noting what you encountered none the less in the hopes that someone else can come along and answer it for you, and the rest of us.</p>

<ul><li>How do we change the location of the <code>struts.xml</code> file?</li><li>How do we set checkboxes to false (on uncheck)?</li><li>How can we set the focus on a form field?</li><li>What is the analogy to ForwardAction?</li><li>How do we extend an action mapping in struts.xml?</li><li>Can we use DynaBeans?</li><li>How do we set a token to track duplicate submits?</li></ul>


<h2 id="Struts1Solutions-Howdowechangethelocationofthestruts.xmlfile?">How do we change the location of the struts.xml file? </h2>

<p>Struts 2 uses a different approach to loading the configuration. The initial <code>struts.xml</code> is loaded from the root of the classpath. The easiest way to set that up is to put the <code>struts.xml</code> under the <code>classes</code> folder. </p>

<p>If maintaining the <code>struts.xml</code> under <code>classes</code> doesn't work for you, then use the "bootstrap" <code>struts.xml</code> to include whatever other <code>struts.xml</code>'s you'd like.</p>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
&lt;struts&gt;
   &lt;include file="..\struts.xml"/&gt;
&lt;/struts&gt;
</pre>
</div></div>

<p>Or, chuck it all and use <a shape="rect" href="zero-configuration.html">Zero Configuration</a> <img class="emoticon emoticon-smile" src="https://cwiki.apache.org/confluence/s/en_GB/5982/f2b47fb3d636c8bc9fd0b11c0ec6d0ae18646be7.1/_/images/icons/emoticons/smile.png" data-emoticon-name="smile" alt="(smile)"></p>

<h2 id="Struts1Solutions-Howdowesetcheckboxesfalse(onuncheck)?">How do we set checkboxes false (on uncheck)?</h2>

<p>In Struts 2, checkboxes are stateful, and we don't need to do that anymore. </p>

<p>Just treat the checkbox like any other tag. The framework will notice if a checkbox is "missing" from a request and inject a false value for that checkbox. </p>

<h2 id="Struts1Solutions-Howcanwesetthefocusonaformfield?">How can we set the focus on a form field?</h2>

<p>Struts 1 generates a little JavaScript that helps set the focus, if you specify the field in the JSP.</p>

<p>Another solution is to use a <a shape="rect" class="external-link" href="http://www.codeproject.com/jscript/FocusFirstInput.asp" rel="nofollow">generic Javascript</a> that automatically seeks the first enabled form field on page.</p>

<p>(<a shape="rect" class="external-link" href="http://issues.apache.org/struts/browse/WW-1350">Building this script into Struts 2 is being considered.</a>)</p>
<ul><li><a shape="rect" class="external-link" href="http://forums.opensymphony.com/thread.jspa?threadID=23777&amp;tstart=0" rel="nofollow">http://forums.opensymphony.com/thread.jspa?threadID=23777&amp;tstart=0</a></li></ul>


<h2 id="Struts1Solutions-WhatistheanalogytoForwardAction?">What is the analogy to ForwardAction?</h2>

<p>The default Action class (ActionSupport) returns SUCCESS by default, and SUCCESS is the default result. Using an action to forward directly to a page is easy:</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
&lt;action name="Welcome" class="com.company.Foo"&gt;
&lt;result&gt;/pages/Welcome.jsp&lt;/result&gt;
&lt;/action&gt;
</pre>
</div></div>

<h2 id="Struts1Solutions-Howdoweemulatethedefault=&quot;true&quot;attributeofaStruts1.xActionmapping?">How do we emulate the default="true" attribute of a Struts 1.x Action mapping?</h2>

<p>Rather than tag the action as being the default, the default action is set by name using <code>default-action-ref</code> element.</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
&lt;default-action-ref name="defaultAction"&gt;
</pre>
</div></div>
<p>By using an element, rather than an attribute, packages can inherit the default action name.</p>

<h2 id="Struts1Solutions-Howdoweextendanactionmappinginstruts.xml?">How do we extend an action mapping in struts.xml?</h2>

<p>Starting in Struts 1.3, we can use the "extends" attribute in our Struts configuration Action mapping to have it inherit properties from a base mapping.  In Struts 2, that technique is no longer necessary because we have packages.  We can create a package, then set for that package the default Result type, Interceptor chain, and global results.  This leaves very little information to actually be included in an action element.</p>

<p>Here is an example of declaring a custom package:</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
&lt;package name="chat" extends="struts-default" namespace="/chat"&gt;
  &lt;global-results&gt;
    &lt;result name="login" type="freemarker"&gt;/chat/chatLogin.ftl&lt;/result&gt;
  &lt;/global-results&gt;
  &lt;default-interceptor-ref name="basicStack"/&gt;
  ...
&lt;/package&gt;
</pre>
</div></div>
<p>In fact, packages themselves can extend other packages, as the "chat" package extends "struts-default" in the above example.</p>

<h2 id="Struts1Solutions-CanweuseDynaBeans?">Can we use DynaBeans?</h2>

<p>Sure, but first, ask "Do we <strong>want</strong> to use DynaBeans at all?" </p>

<p>Typically, DynaBeans are used to emulate a plain old JavaBean that is utilized by the business logic. If that is the case, then we can just use the plain old JavaBean directly. Struts 2 will happily cope with rich properties, like date fields. We don't have to reduce everything to a String anymore. </p>

<p>Otherwise, DynaBeans can be treated as a regular Java object, if the particular implementation contains a getMap() method.  This method lets OGNL, the Struts 2 expression language, know how to access and set data.  DynaBean variants like the LazyDynaBean can create themselves on-the-fly, where other more static types might need to be created in your Action's constructor before being used.  </p>

<h2 id="Struts1Solutions-Howdowesetatokentotrackduplicatesubmits?">How do we set a token to track duplicate submits? </h2>

<p>In Struts 2,  there shouldn't be any token code in an Action class at all. The Interceptors are designed to do all the work. There are three flavors, the Token Interceptor, the Token Session Interceptor, and the Execute and Wait Interceptor.</p>

<h3 id="Struts1Solutions-TokenInterceptor">Token Interceptor </h3>

<p>The <a shape="rect" href="token-interceptor.html">Token Interceptor</a> is most like the Struts 1 approach, except that we don't have to change the Action class to add a lot of busy code. The trade off is that we  <strong>do</strong> have to include the <a shape="rect" href="token.html">token</a> tag in the form, to bootstrap the process. In Struts 1, we set the token in the Action, and the form tag detected it. In Struts 2, we set the token in the page, and the Interceptor detects it. </p>

<p>If the Interceptor does detect a duplicate submit, then it automatically returns an "invalid.token" result code, which can be handled via the action mapping. </p>

<h3 id="Struts1Solutions-TokenSession">Token Session </h3>

<p>The <a shape="rect" href="token-session-interceptor.html">Token Session Interceptor</a> tries to be even more automatic by attempting to display the same response that the original, valid action invocation would have displayed if no multiple requests were submitted in the first place.</p>

<h3 id="Struts1Solutions-ExecuteandWait">Execute and Wait </h3>

<p>The <a shape="rect" href="execute-and-wait-interceptor.html">Execute and Wait Interceptor</a> embraces long-running taks by presenting a progress meter as the Action runs in the background. </p>

<h2 id="Struts1Solutions-Next:">Next: <a shape="rect" href="what-is-the-actioncontext.html">What is the ActionContext?</a></h2>


<hr>
<p>This material originally adopted from: <a shape="rect" class="external-link" href="http://wiki.apache.org/struts/IssuesAndSolutions">http://wiki.apache.org/struts/IssuesAndSolutions</a>.</p></div>
        </div>

        
    </div>
</div>
<div class="footer">
    Generated by CXF SiteExporter
</div>
</body>
</html>
